package mya.mes.mdc.mqtt;


import mya.mes.mdc.share.api.MdcModule;
import mya.mes.mdc.share.mqttwrite.MqttClientPublisher;
import mya.mes.mdc.share.vo.MdcOptions;

public class MqttClientManager {


    private static final MqttClientManager _instance = new MqttClientManager();

    private final MqttClientEngine mqttClientEngine;

    private final FuseMqttClient fuseMqttClient;

    private MdcOptions mdcOptions;

    private final DataConverter dataConverter = new DataConverter();

    private final MqttReceivedQueue mqttMsgQueue;
    private final MsgReceiver msgReceiver;
    private MqttClientManager() {

        MdcOptions mdcOptions = MdcModule.getInstance().getMdcOptions();

        mqttMsgQueue = new MqttReceivedQueue();

        this.msgReceiver = new MsgReceiver(mdcOptions, mqttMsgQueue);

        fuseMqttClient = new FuseMqttClient(mdcOptions, mqttMsgQueue, msgReceiver);
        mqttClientEngine = new MqttClientEngine(mdcOptions, fuseMqttClient);

    }


    public void start() {
        fuseMqttClient.connect();
    }

    public static MqttClientManager getInstance() {
        return _instance;
    }


    public MqttClientPublisher getMqttClientPublisher() {
        return mqttClientEngine;
    }


    public DataConverter getDataConverter() {
        return dataConverter;
    }


    public void shutdown() throws Exception {
        fuseMqttClient.shutdown();
    }
}
